package controller;

import java.util.Locale;

import com.actionbarsherlock.app.SherlockFragment;
import com.example.remotecontrol.R;
import components.FragmentMediator;

import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ViewPagerFragment extends SherlockFragment implements TabListener {

	private Fragment[] tabFragment;
	private Integer[] tabTitles = new Integer[] { R.string.action,
			R.string.sms_log, R.string.setting };
	
	public ViewPagerFragment() {
		// TODO Auto-generated constructor stub
		this.tabFragment = new Fragment[3];
		this.tabFragment[0] = FragmentMediator.CreateFragment(FragmentMediator.FRAGMENT_HOME);
		this.tabFragment[2] = FragmentMediator.CreateFragment(FragmentMediator.FRAGMENT_SETTING);
		this.tabFragment[1] = FragmentMediator.CreateFragment(FragmentMediator.FRAGMENT_SMS_LOG_MANAGER);
	}
	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.tab_navigation, container,
				false);
		// Set up the action bar.
		final ActionBar actionBar = getActivity().getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSherlockActivity().getSupportActionBar().setDisplayShowHomeEnabled(true);
getSherlockActivity().getSupportActionBar().setDisplayShowTitleEnabled(true);

		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				 getChildFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) view.findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		// When swiping between different sections, select the corresponding
		// tab. We can also use ActionBar.Tab#select() to do this if we have
		// a reference to the Tab.
		mViewPager
				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						actionBar.setSelectedNavigationItem(position);
						getActivity().invalidateOptionsMenu();
					}
				});

		
		// For each of the sections in the app, add a tab to the action bar.
		actionBar.removeAllTabs();
		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
			// Create a tab with text corresponding to the page title defined by
			// the adapter. Also specify this Activity object, which implements
			// the TabListener interface, as the callback (listener) for when
			// this tab is selected.
			actionBar.addTab(actionBar.newTab()
					.setText(mSectionsPagerAdapter.getPageTitle(i))
					.setTabListener(this));
		}
		
		return view;
	}


	@Override
	public void onTabSelected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
		// When the given tab is selected, switch to the corresponding page in
		// the ViewPager.
		
		mViewPager.setCurrentItem(tab.getPosition(), true);
		
	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
		//mViewPager.setCurrentItem(tab.getPosition(), true);
	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentStatePagerAdapter  {


		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int position) {
			// getItem is called to instantiate the fragment for the given page.
			// Return a DummySectionFragment (defined as a static inner class
			// below) with the page number as its lone argument.
			Fragment fragment = tabFragment[position];
			return fragment;
		}

		@Override
		public int getCount() {
			// Show 3 total pages.
			return tabFragment.length;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			
			return getActivity().getString(tabTitles[position]).toUpperCase(l);
		}
		
//		@Override
//		public int getItemPosition(Object object) {
//			// TODO Auto-generated method stub
////			 if (object instanceof FirstPageFragment && mFragmentAtPos0 instanceof NextFragment)
////		            return POSITION_NONE;
//	//	        return POSITION_UNCHANGED;
//			 return POSITION_NONE;
//		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			super.destroyItem(container, position, object);
		}
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		final ActionBar actionBar = getActivity().getActionBar();
	
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		final ActionBar actionBar = getActivity().getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
	}
	

	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
	}
//	@Override
//	public void setUserVisibleHint(boolean isVisibleToUser) {
//		// TODO Auto-generated method stub
//		super.setUserVisibleHint(isVisibleToUser);
//		
//		final ActionBar actionBar = getActivity().getActionBar();
//		
//		if (isVisibleToUser) {
//			actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//		} else {
//			
//		}
//	} 
}
